package school.xauat.offershow.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import school.xauat.offershow.pojo.LoginResponse;
import school.xauat.offershow.pojo.User;
import school.xauat.offershow.serice.UserService;

import javax.servlet.http.HttpSession;

/**
 * @author ：zsy
 * @date ：Created 2021/12/19 20:05
 * @description：
 */
@RestController()
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ResponseBody
    public LoginResponse login(@RequestBody User curUser,
                               HttpSession session) {
        User user = userService.checkUser(curUser.getUsername(), curUser.getPassword());
        LoginResponse loginResponse = new LoginResponse();
        if (user != null) {
            user.setPassword(null);
            session.setAttribute("user", user);
            loginResponse.setSuccess(true);
            loginResponse.setMessage(userService.getUserInfo(user.getId(), "publish_time"));
        } else {
            loginResponse.setSuccess(false);
            loginResponse.setReason("用户名或密码错误");
        }
        return loginResponse;
    }

    @GetMapping("/loginStatus")
    public LoginResponse getCurLoginStatus(HttpSession session) {
        LoginResponse response = new LoginResponse();
        response.setSuccess(session.getAttribute("user") != null);
        return response;
    }

    @PostMapping("/register")
    public LoginResponse register(@RequestBody User curUser,
                                  HttpSession session) {
        String username = curUser.getUsername();
        String password = curUser.getPassword();
        LoginResponse response = new LoginResponse();
        if (username == null || "".equals(username)) {
            response.setSuccess(false);
            response.setReason("用户名为空");
        } else if (password == null || "".equals(password)) {
            response.setSuccess(false);
            response.setReason("密码为空");
        } else if (username.equals(userService.checkUsername(username))) {
            response.setSuccess(false);
            response.setReason("用户名已经存在了");
        } else {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            int add = userService.add(user);
            if (add != 1) {
                response.setSuccess(false);
                response.setReason("插入失败，未知错误");
            } else {
                response.setSuccess(true);
                session.setAttribute("user", user);
            }
        }
        return response;
    }

    @GetMapping("/logout")
    public void logout(HttpSession session) {
        session.removeAttribute("user");
    }

    @GetMapping("/checkUsername")
    public String checkUsername(@RequestParam String username) {
        return userService.checkUsername(username);
    }

    @GetMapping("/getUserInfo")
    public LoginResponse getUserInfo(
            HttpSession session,
            @RequestParam(defaultValue = "publish_time") String order) {
        LoginResponse response = new LoginResponse();
        if (session.getAttribute("user") == null) {
            response.setSuccess(false);
            response.setReason("当前状态:未登录");
        } else {
            User user = (User) session.getAttribute("user");
            response.setSuccess(true);
            response.setMessage(userService.getUserInfo(user.getId(), order));
        }
        return response;
    }
}
